package br.edu.univas.locadoragraphtcc.database;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class Connection {
	
	private static final String path = "database";
	private static Connection conn = null;
	
	private Connection(){}
	
	public static Connection getInstance(){
		if (conn == null) 
			conn = new Connection();
		return conn;
	}
	
	public GraphDatabaseService newGraphDatabase(){
		GraphDatabaseService graph = new GraphDatabaseFactory().newEmbeddedDatabase(path);
		registerShutdownHook(graph);
		return graph;
	}
	
	private void registerShutdownHook(final GraphDatabaseService graph) {
		Runtime.getRuntime().addShutdownHook(new Thread(){
			@Override
			public void run() {
				graph.shutdown();
			}
		});
		
	}
	
	

}
